package com.java.multipledatasource.thread;

/**
 * @author hu
 * @version 1.0
 * @date 2021/1/19 0019 上午 9:26
 * @description
 */
public class WaitNotify {

    final  static  Object obj  = new Object();
    static volatile int count = 0;
    static class Product implements Runnable{

        @Override
        public void run() {
            synchronized (obj) {
                for (int i = 0; i < 10; i++) {
                    count++;
                    if (count == 5) {
                        obj.notify();
                        try {
                            obj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

    static class  Watch implements Runnable{

        @Override
        public void run() {
           synchronized (obj) {
               while (count != 5) {
                   try {
                       obj.wait();
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
               }
               obj.notify();
               System.out.println("watch end");
           }
        }
    }

    public static void main(String[] args) {
        new Thread(new Product()).start();
        new Thread(new Watch()).start();
    }
}
